package com.howells.time.module.register.view;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.howells.time.R;
import com.howells.time.base.activity.BaseActivity;
import com.howells.time.base.utils.ToastShow;
import com.howells.time.module.register.presenter.ResigisterPresenter;
import com.howells.time.module.register.presenter.ResigisterPresenterImp;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2016.4.11.
 */
public class ForgetPswActivity extends BaseActivity implements ResigisterView{

    private ResigisterPresenter resigisterPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repsw);
        resigisterPresenter=new ResigisterPresenterImp(this);
        ((TextView)findViewById(R.id.top_pannel_title)).setText("修改密码");
    }

    public void onClick(View view){
        switch (view.getId()){
            case R.id.top_pannel_back:
                finish();
                break;
        }
    }

    public void sendcode(View view){
        final TextView send=(TextView)findViewById(R.id.send_check_code);
        TextView phonetext= (TextView) findViewById(R.id.input_number);
        if(phonetext.length()!=11){
            ToastShow.ToastShort(this,"请输入正确手机号");
            return;
        }
        send.setEnabled(false);
        final Timer timer=new Timer();
        final Handler handler=new Handler() {

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what>=0){
                    send.setText(Integer.toString(msg.what)+"秒");
                }else {
                    timer.cancel();
                    send.setText("发送验证码");
                    send.setEnabled(true);
                }
            }
        };
        timer.schedule(new TimerTask() {
            int i = 60;

            @Override
            public void run() {
                Message msg = new Message();
                msg.what = i--;
                handler.sendMessage(msg);
            }
        }, 1000, 1000);
        resigisterPresenter.getVerifyCode(phonetext.getText().toString());
    }

    public void forgetpsw(View view){
        TextView phonetext= (TextView) findViewById(R.id.input_number);
        if(phonetext.getText().length()!=11){
            ToastShow.ToastShort(this,"请输入正确手机号");
            return;
        }
        TextView codetext= (TextView) findViewById(R.id.input_code);
        if(codetext.getText().length()==0){
            ToastShow.ToastShort(this,"请输入验证码");
            return;
        }
        TextView pswtext= (TextView) findViewById(R.id.input_psw);
        if(pswtext.getText().length()==0){
            ToastShow.ToastShort(this,"请输入密码");
            return;
        }
        resigisterPresenter.forgetpsw(phonetext.getText().toString(), codetext.getText().toString(), pswtext.getText().toString());
    }

    @Override
    public void Success() {
        ToastShow.ToastShort(this,"修改成功");
        this.finish();
    }


}
